package com.sczmgk.hotline_pzh.view;


import com.sczmgk.hotline_pzh.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class ConstomStarView extends ImageView {

	public static final int LEVEL_1 = 1;
	public static final int LEVEL_2 = 2;
	public static final int LEVEL_3 = 3;
	public static final int LEVEL_4 = 4;
	public static final int LEVEL_5 = 5;
	private float w;
	private float h;
	private String TAG = "ConstomImageView";

	public ConstomStarView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public ConstomStarView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ConstomStarView(Context context) {
		super(context);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {int action = event.getAction();
	switch (action) {
	case MotionEvent.ACTION_DOWN:
		break;
	case MotionEvent.ACTION_MOVE:
		float x3 = event.getX();
		if (x3  > getCurrentLocation(7)) {
			setImageResource(R.drawable.star_5);
		} else if (x3  > getCurrentLocation(5)) {
			setImageResource(R.drawable.star_4);
		} else if (x3  > getCurrentLocation(3)) {
			setImageResource(R.drawable.star_3);
		} else if (x3  > getCurrentLocation(1)) {
			setImageResource(R.drawable.star_2);
		} else {
			setImageResource(R.drawable.star_1);
		}
		break;
	case MotionEvent.ACTION_UP:
		float x4 = event.getX();
		if (x4  > getCurrentLocation(7)) {
			setImageResource(R.drawable.star_5);
			if(changeListener!= null){
				changeListener.onTimeChenged(StarLevelEnum.level_5);
			}
		} else if (x4 > getCurrentLocation(5)) {
			setImageResource(R.drawable.star_4);
			if(changeListener!= null){
			changeListener.onTimeChenged(StarLevelEnum.level_4);
			}
		} else if (x4  > getCurrentLocation(3)) {
			setImageResource(R.drawable.star_3);
			if(changeListener!= null){
			changeListener.onTimeChenged(StarLevelEnum.level_3);
			}
		} else if (x4  > getCurrentLocation(1)) {
			setImageResource(R.drawable.star_2);
			if(changeListener!= null){
			changeListener.onTimeChenged(StarLevelEnum.level_2);
			}
		} else {
			setImageResource(R.drawable.star_1);
			if(changeListener!= null){
			changeListener.onTimeChenged(StarLevelEnum.level_1);
			}
		}
		break;

	default:
		break;
	}
	return true;}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		w = this.getWidth();
		h = this.getHeight();
	}
	/**
	 * 设置显示星级
	 * @param starLevel
	 */
	public void setStarLevel(int starLevel){
		switch (starLevel) {
		case LEVEL_1:
			setImageResource(R.drawable.star_1);
			break;
		case LEVEL_2:
			
			setImageResource(R.drawable.star_2);
			break;
		case LEVEL_3:
			setImageResource(R.drawable.star_3);
			
			break;
		case LEVEL_4:
			
			setImageResource(R.drawable.star_4);
			break;
		case LEVEL_5:
			setImageResource(R.drawable.star_5);
			
			break;

		default:
			break;
		}
	}
	public interface onStarChangeListener{
		void onTimeChenged(StarLevelEnum starLevel);
	}
	private onStarChangeListener changeListener;

	public void setChangeListener(onStarChangeListener changeListener) {
		this.changeListener = changeListener;
	}
	private float getCurrentLocation(int position){
		float dis = (w/8);//每一段的距离
		float curDis = dis * position;
		return curDis;
	}
}

